from django.db import models
from django.core.urlresolvers import reverse
from django.utils import timezone

# Create your models here.
"""
博客的模型
"""


# 文章
class Article(models.Model):
    # 博客标题
    title = models.CharField(max_length=256)
    # 博客标签
    category = models.CharField(default="未标签", max_length=50, blank=True)
    # 文章时间
    dateTime = models.DateTimeField(auto_now=True)
    # 文章内容
    content = models.TextField(blank=True, null=True)
    # 作者
    author = models.CharField(default="匿名", max_length=50)

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse("blog:detail", kwargs={"postId": self.id})

    # 按时间降序
    class Meta:
        ordering = ['-dateTime']
#         verbose_name = "Article"
#         verbose_name_plural = "Articles"


class CommentManager(models.Manager):

    def create_comment(self,  postid, nickName, content):

        article = Article.objects.get(pk=postid)
        comment = self.create(
            atricle=article, nickName=nickName, content=content)
        comment.save()
        return comment


# 评论
class Comment(models.Model):
    # 评论人昵称
    nickName = models.CharField(max_length=32, default="游客")
    # 评论内容
    content = models.TextField(blank=False, null=False)
    # 评论时间
    dateTime = models.DateTimeField(auto_now=True)

    # 一对多的方式和文章关联起来
    atricle = models.ForeignKey('Article', null=True)

    # 管理对象的创建
    objects = CommentManager()

    # 评论排序
    class Meta:
        ordering = ['dateTime']

    def __str__(self):
        return self.content


# class CommentReplyManager(models.Manager):
#
#     def crete_commentreply(self,  commentId, reply, nickName):
#         comment = Comment.objects.get(pk=commentId)
#         reply = self.create(
#             comment=comment, replyContent=reply, nickName=nickName)
#         reply.save()
#         return reply


# class CommentReply(models.Model):
#
#     # 评论内容
#     comment = models.ForeignKey(
#         'Comment',  null=False, blank=False)
#     replyContent = models.TextField(blank=False, null=False, default=None)
#
#     # 评论人昵称
#     nickName = models.CharField(max_length=32, default="游客")
#     # 评论时间
#     dateTime = models.DateTimeField(auto_now=True)
#
#     # 管理对象的创建
#     objects = CommentReplyManager()
#
# #     @classmethod
# #     def create(cls, commentId, reply, nickName):
# #         comment = Comment.objects.get(pk=commentId)
# #         reply = cls(comment=comment, replyContent=reply, nickName=nickName)
# #         reply.save()
# #         return reply
#
#     def __str__(self):
#         return self.replyContent
